/* Copyright (c) 2014 Affiliated Computer Services, A Xerox Company.
 * All rights reserved.
 *
 * This item contains confidential and trade secret information and may not
 * be transferred, published, disclosed, reproduced, or used by any party
 * without the express written permission of Affiliated Computer Services.
 */
package org.jarvis.test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;

import org.apache.commons.lang.StringUtils;

/**
 * @author Jarvis Su
 * @created May 13, 2015
 *
 */
public class DataPre {

	static List<Long> providers;
	static List<Long> cases;
	static Individual individual;
	static List<Long> auth;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Long number = 2000000l;
		Date processDate = new Date();
		providers = genProvidersFile(number, processDate);
		cases = genCaseFile(number, processDate);
		individual = genIndividualFile(number, processDate);
		auth = genAuthFile(number, processDate);

	}

	private static List<Long> genAuthFile(Long number, Date processDate) {

		List<Long> auths = new ArrayList<Long>();
		Long authNumMax = 1234567890001l;
		String authNum = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");

		System.out.println(sdf.format(processDate));
		File dest = new File("E:/tmp/JFS.TEST.AUTH.D" + sdf.format(processDate));
		BufferedWriter writer = null;
		try {
			writer = new BufferedWriter(new FileWriter(dest));
			String header = "IA0" + sdf1.format(processDate)
					+ StringUtils.leftPad(number.toString(), 9, '0')
					+ StringUtils.leftPad("", 180, ' ');
			String footer = "IA9" + sdf1.format(processDate)
					+ StringUtils.leftPad("", 189, ' ');
			writer.write(header);
			writer.newLine();

			Random random1 = new Random();
			Random random2 = new Random();
			Random random3 = new Random();

			Calendar c = Calendar.getInstance();

			for (long i = 0; i < number; i++) {

				if (i % 300 == 0) {
					c.set(Calendar.YEAR, 2014);
					c.set(Calendar.MONTH, 0);
					c.set(Calendar.DAY_OF_MONTH, 1);
					c.set(Calendar.HOUR_OF_DAY, 0);
					c.set(Calendar.MINUTE, 0);
					c.set(Calendar.SECOND, 0);
					c.set(Calendar.MILLISECOND, 0);
				}
				c.add(Calendar.DATE, (int) i);
				Date date = c.getTime();

				int index = Math.abs(random1.nextInt()) % cases.size();
				int pIndex = Math.abs(random2.nextInt()) % providers.size();
				int cIndex = Math.abs(random3.nextInt())
						% individual.getChildren().size();
				Long caseNum = cases.get(index);
				Long provider = providers.get(pIndex);
				Long child = individual.getChildren().get(cIndex);

				Long authLong = authNumMax + i;
				authNum = authLong.toString();
				StringBuffer d = new StringBuffer("IA1");
				d.append(sdf1.format(processDate));
				d.append(StringUtils.leftPad(authNum, 13, '0'));
				d.append(caseNum);
				d.append(provider);
				d.append(sdf1.format(date));
				d.append(sdf1.format(date));
				d.append("FT");
				d.append("000002000");
				d.append(child);
				d.append("0030");
				d.append("100");
				d.append(StringUtils.rightPad("", 105, ' '));

				writer.write(d.toString());
				writer.newLine();
				auths.add(authLong);
			}
			writer.write(footer);
			writer.newLine();
			writer.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				writer.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return auths;

	}

	private static Individual genIndividualFile(Long number, Date processDate) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");

		System.out.println(sdf.format(processDate));
		File dest = new File("E:/tmp/JFS.TEST.INDV.D" + sdf.format(processDate));
		BufferedWriter writer = null;

		Individual individual = new Individual();
		List<Long> persons = new ArrayList<Long>();
		List<Long> children = new ArrayList<Long>();
		Long ssnNumMax = 184748567l;
		Long altMax = 100422531277l;
		String ssn = "";
		String alt = "";

		try {
			writer = new BufferedWriter(new FileWriter(dest));
			String header = "CI0" + sdf1.format(processDate)
					+ StringUtils.leftPad(number.toString(), 6, '0')
					+ StringUtils.leftPad("", 183, ' ');
			String footer = "CI9" + sdf1.format(processDate)
					+ StringUtils.leftPad("", 189, ' ');
			writer.write(header);
			writer.newLine();
			for (long i = 0; i < number; i++) {
				Long altLong = altMax + i;
				alt = altLong.toString();

				Long ssnLong = ssnNumMax + i;
				ssn = ssnLong.toString();

				StringBuffer d = new StringBuffer("CI1");
				d.append(sdf1.format(processDate));
				d.append(StringUtils.leftPad(alt, 12, '0'));
				d.append(cases.get(new Long(i).intValue()).toString());
				d.append(StringUtils.leftPad("FIRST-" + i, 15, ' '));
				d.append(StringUtils.leftPad("LAST-" + i, 15, ' '));
				d.append("M");
				d.append(StringUtils.leftPad(ssn, 9, '0'));
				d.append("20010101");
				d.append("A");
				if (i % 2 == 0) {
					d.append("PC");
					persons.add(altLong);
				} else {
					d.append("CC");
					children.add(altLong);
				}
				d.append("Y");
				d.append("Y");
				d.append("01");
				d.append("10");
				d.append("Y");
				d.append(StringUtils.leftPad("", 109, ' '));

				writer.write(d.toString());
				writer.newLine();
			}
			writer.write(footer);
			writer.newLine();
			writer.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				writer.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		individual.setPersons(persons);
		individual.setChildren(children);
		return individual;
	}

	private static List<Long> genCaseFile(Long number, Date processDate) {
		List<Long> cases = new ArrayList<Long>();
		Long caseNumMax = 6004118840l;
		String caseNum = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");

		System.out.println(sdf.format(processDate));
		File dest = new File("E:/tmp/JFS.TEST.CASE.D" + sdf.format(processDate));
		BufferedWriter writer = null;
		try {
			writer = new BufferedWriter(new FileWriter(dest));
			String header = "CA0" + sdf1.format(processDate)
					+ StringUtils.leftPad(number.toString(), 6, '0')
					+ StringUtils.leftPad("", 233, ' ');
			String footer = "CA9" + sdf1.format(processDate)
					+ StringUtils.leftPad("", 239, ' ');
			writer.write(header);
			writer.newLine();
			for (long i = 0; i < number; i++) {
				Long caseIdLong = caseNumMax + i;
				caseNum = caseIdLong.toString();
				StringBuffer d = new StringBuffer("CA1");
				d.append(sdf1.format(processDate));
				d.append(StringUtils.leftPad(caseNum, 10, '0'));
				d.append("20150101");
				d.append("A");
				d.append("20991231");
				d.append(StringUtils.rightPad("ADDR-LINE-01-" + caseNum, 40,
						' '));
				d.append(StringUtils.rightPad("ADDR-LINE-02-" + caseNum, 40,
						' '));
				d.append(StringUtils.rightPad("city-" + caseNum, 15, ' '));
				d.append("OH");
				d.append("123410000");
				d.append(StringUtils.rightPad(number.toString(), 10, '0'));
				d.append("031");
				d.append("Y");
				d.append("FT");
				d.append("0215");
				d.append(StringUtils.leftPad("", 87, ' '));

				writer.write(d.toString());
				writer.newLine();
				cases.add(caseIdLong);
			}
			writer.write(footer);
			writer.newLine();
			writer.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				writer.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return cases;
	}

	public static List<Long> genProvidersFile(Long number, Date processDate) {
		List<Long> providers = new ArrayList<Long>();
		Long providerNumMax = 123450000008557l;
		String providerNum = "";

		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");

		System.out.println(sdf.format(processDate));
		File dest = new File("E:/tmp/JFS.TEST.PROVIDER.D"
				+ sdf.format(processDate));
		BufferedWriter writer = null;
		try {
			writer = new BufferedWriter(new FileWriter(dest));
			String header = "PR0"
					+ sdf1.format(processDate)
					+ StringUtils.leftPad(number.toString(), 6, '0')
					+ "                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         ";
			String footer = "PR9"
					+ sdf1.format(processDate)
					+ "                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               ";
			writer.write(header);
			writer.newLine();
			for (long i = 0; i < number; i++) {
				Long providerIdLong = providerNumMax + i;
				providerNum = providerIdLong.toString();
				// StringUtils.leftPad(providerNum, 10);
				String providerDetails = "PR1"
						+ sdf1.format(processDate)
						+ providerNum
						+ "AECCOH-PROVANAME1                                  0PROV FIRSTNAME1  PROV LASTNAME1    A2020090498765432100001PROV1-ADDR-LINE-1                       PROVADDR-LINE-12                      09PROVADDR-CITY1 OH99999000101987654321444444444PROV-MAIL -ADDR-LINE 1                  PROV-MAILADDR-LINE11                    PROVMAIL-CITY1 OH343330000PROV1@EMAIL.ADDR                                                      222201                                                                                                                                                                                                              ";
				writer.write(providerDetails);
				writer.newLine();
				providers.add(providerIdLong);
			}
			writer.write(footer);
			writer.newLine();
			writer.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				writer.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return providers;
	}

	public static void genFiles(long number) {
		File file = new File("E:/tmp/reports.properties");
		File dest = new File("E:/tmp/new.txt");
		try {
			BufferedReader reader = new BufferedReader(new FileReader(file));
			BufferedWriter writer = new BufferedWriter(new FileWriter(dest));
			String line = reader.readLine();
			while (line != null) {
				writer.write(line);
				writer.newLine();
				line = reader.readLine();
			}
			writer.flush();
			reader.close();
			writer.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
